
***************************************
*          @@  FLEX 3 @@              *                  
*                                     *
*   ------ >> Chapitre 14 << ------   *
*                                     *
***************************************




java version
Echo %JAVA_HOME%
public class ClasseJ2ee{

	// Mthode
	public int calculer (int a, int b)
	{
	    return (a + b);
	}

}
javac ClasseJ2ee.java
<?xml version="1.0" encoding="UTF-8"?>

<service id="remoting-service"
    class="flex.messaging.services.RemotingService">

    <adapters>
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>

    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>

    <destination id="destJ2ee">
	<properties>
	    <source>ClasseJ2ee</source>
	    <scope>application</scope>
	</properties>

    </destination>

</service>
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

	<mx:Panel x="10" y="10" width="250" height="200" layout="absolute" title="Addition">
	    <mx:Form x="10" y="10" height="107" width="210" horizontalScrollPolicy="off" verticalScrollPolicy="off">
	        <mx:FormItem label="Entier A: ">
	            <mx:TextInput width="78" id="txt_entiera"/>
	        </mx:FormItem>
	        <mx:FormItem label="Entier B:">
	            <mx:TextInput width="78" id="txt_entierb"/>
	        </mx:FormItem>
	        <mx:FormItem label="Rsultat :">
	            <mx:TextInput width="79" id="txt_resultat"/>
	        </mx:FormItem>
	    </mx:Form>

	    <mx:Button x="10" y="128" label="Calculer" width="210" />
	</mx:Panel>
	
</mx:Application>
<mx:RemoteObject id="roMethodeJ2ee"
	    destination="destJ2ee"
	    result="resultatOK(event)"
	    fault="resultatKO(event)" showBusyCursor="true">
	</mx:RemoteObject>
<mx:Script>

	    <![CDATA[
        import mx.rpc.events.FaultEvent;
	        import mx.rpc.events.ResultEvent;
	        import mx.controls.Alert;
	
	        public function executerMethodeJ2ee():void
	        {
	            var a:int = int(txt_entiera.text);
	            var b:int = int(txt_entierb.text);
	            roMethodeJ2ee.calculer(a,b);
	        }
	
	        public function resultatOK(e:ResultEvent):void
	        {
	            txt_resultat.text = e.result.toString();
	        }

	        public function resultatKO(e:FaultEvent):void
	        {
	            Alert.show(e.fault.faultString);
	        }
	    ]]>

</mx:Script>
<mx:Button x="10" y="128" label="Calculer" width="210" click="executerMethodeJ2ee()"/>
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

	<mx:Panel x="10" y="10" width="250" height="335" layout="absolute">
	    <mx:Canvas x="10" y="10" width="210" height="200" backgroundColor="#BFB5B5">

	        <!-- ROBOT !-->
        <mx:Canvas x="90" y="81" width="30" height="30" backgroundColor="#F90101" cornerRadius="5" borderStyle="solid" id="irobot">
	        </mx:Canvas>

	    </mx:Canvas>
	
	    <mx:Button x="82.5" y="218" label="Haut" width="70" id="btn_haut"/>
	    <mx:Button x="82.5" y="263" label="Bas" width="70" id="btn_bas"/>
	    <mx:Button x="153" y="241" label="Droite" width="70" id="btn_droite"/>
	    <mx:Button x="10" y="241" label="Gauche" width="70" id="btn_gauche"/>

	</mx:Panel>
	
</mx:Application>
<services>
    <service-include file-path="remoting-config.xml"/>
    <service-include file-path="proxy-config.xml"/>
    <service-include file-path="messaging-config.xml"/>
    </services>
<channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
    <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
    <properties>
        <polling-enabled>true</polling-enabled>
        <polling-interval-seconds>4</polling-interval-seconds>
    </properties>
</channel-definition>
<?xml version="1.0" encoding="UTF-8"?>

<service id="message-service"
    class="flex.messaging.services.MessageService">

    <adapters>
        <adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />
        <!-- <adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/> -->
    </adapters>

    <default-channels>
        <channel ref="my-polling-amf"/>
    </default-channels>

    <destination id="bougerrobot"/> 

</service>
<mx:Consumer id="ecouteur" destination="bougerrobot" message="receptionner(event.message)"/>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="emetteur.subscribe()">
<mx:Producer id="emetteur" destination="bougerrobot"/>
<mx:Script>
	    <![CDATA[
	        import mx.messaging.messages.AsyncMessage;
	        import mx.messaging.messages.IMessage;

	        private function envoyer(direction:String):void{
	            var messageAsynchrone:IMessage = new AsyncMessage();
	            messageAsynchrone.body.chatMessage = direction;
	            emetteur.send(messageAsynchrone);
	        }
	    ]]>
</mx:Script>
private function receptionner(messageDestination:IMessage):void{
	    var directionRecue:String = messageDestination.body.chatMessage;
	
	    switch (directionRecue) {
	
	        case "G" : gauche();
	            break;
	 
	        case "D" : droite();
	            break;
	 
	        case "H" : haut();
	            break;
	 
	        case "B" : bas();
	            break;
	    }
	}
public function gauche():void
	{
	    irobot.x = irobot.x-5
	}

	public function droite():void
	{
	    irobot.x = irobot.x+5
}
	
	public function haut():void
	{
	    irobot.y = irobot.y-5
	}
	
	public function bas():void
	{
	    irobot.y = irobot.y+5
	}
